from leetcode_test import TreeNode, ListNode
class Solution:
    def sortedListToBST(self, head: ListNode) -> TreeNode:

        l = []
        while head:
            l.append(head.val)
            head = head.next

        length = len(l)

        left = 0
        right = length

        def build_tree(left, right):
            if left == right:
                return 
            mid = (left + right) // 2
            tree = TreeNode(l[mid])

            tree.left = build_tree(left, mid)
            tree.right = build_tree(mid+1, right)
            return tree
        
        return build_tree(left, right)


list_node = ListNode.create([-10, -3, 0, 5, 9])
tree = Solution().sortedListToBST(list_node)
print(tree.get_value())

